package com.jymj.controller;


import com.jymj.entity.*;
import com.jymj.entity.huji.FeiZhaiEntity;
import com.jymj.entity.huji.ZhaiEntity;
import com.jymj.entity.landmanage.PjEnviorSupvsEntity;
import com.jymj.entity.model.BplxEntity;
import com.jymj.entity.model.RjhjhpEntity;
import com.jymj.entity.model.RjdlEntity;
import com.jymj.entity.model.RjriverEntity;
import com.jymj.service.*;
import com.jymj.utils.R;
import com.jymj.utils.ShiroUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by ljj on 2017/12/18.
 */
@RestController
@RequestMapping("/point")
public class PointController extends AbstractController {
    
    @Resource
    private ZhaiService zhaiService;
    @Resource
    private FeiZhaiService feiZhaiService;
    @Resource
    private YlService ylService;
    @Resource
    private PointService pointService;
    @Resource
    private SysXzqService sysXzqService;
    @Resource
    private CJService cjService;
    @Resource
    private EnviorSupvsService enviorSupvsService;

    /**
     * 点查询 查询院落 户籍 房屋------1
     *
     * @param point
     * @return
     */
    @RequestMapping(value = "/getQueryPoint")
    @ResponseBody
    public R getQueryPoint(String point) {
        Map map;
        try {
            SysUserEntity user = ShiroUtils.getUserEntity();
            map = pointService.getQueryPoint(point, user.getCode());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return R.error("查询失败！");
        }
        return R.ok().put("data", map);
    }

    /**
     * 框选 统计 男 女 18 65 宅基地 200 267 就业 户别-----1
     *
     * @param points
     * @return
     */
    @RequestMapping(value = "/getAppQueryFrame")
    @ResponseBody
    public R getAppQueryFrame(String points) {
        Map map;
        try {
            SysUserEntity user = ShiroUtils.getUserEntity();
            map = pointService.getAppQueryFrame(points, user.getCode());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return R.error("查询失败！");
        }
        return R.ok().put("data", map);
    }

    //框选yl-----1
    @RequestMapping(value = "/getYlQueryFrame")
    @ResponseBody
    public R getYlQueryFrame(Integer pageSize, Integer currPage, String points) {
        Map map;
        try {
            SysUserEntity user = ShiroUtils.getUserEntity();
            map = pointService.getYlQueryFrame(pageSize, currPage, points, user.getCode());
        } catch (Exception e) {
            logger.error(e.getMessage());
            return R.error("查询失败！");
        }
        return R.ok().put("data", map);
    }

    //-------1
    @RequestMapping(value = "/getZhaiByYl")
    @ResponseBody
    public R getZhaiByYl(Integer objectid) {
        YlEntity ylEntity;
        Map map = new HashMap();
        try {
            ylEntity = ylService.getYLbyId(objectid, null);
            map.put("ylEntity", ylEntity);
            if (ylEntity.getYlType() == 1 || ylEntity.getYlType() == 4) {
                List<ZhaiEntity> zhaiList = zhaiService.getHujiByObid(objectid);
                map.put("zhaiList", zhaiList);
            } else {
                List<FeiZhaiEntity> feiZhaiList = feiZhaiService.getFeiZhaiEn(objectid);
                map.put("feiZhaiList", feiZhaiList);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return R.error("查询失败！");
        }
        return R.ok().put("data", map);
    }

    /**
     * 框选 yl 做导出使用
     *
     * @param points
     * @return
     */
    @RequestMapping(value = "/getExcelYlFrame")
    @ResponseBody
    public R getExcelYlFrame(String points) {
        Map map;
        try {
            SysUserEntity user = ShiroUtils.getUserEntity();
            map = pointService.getYlQueryFrame(null, null, points, user.getCode());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return R.error("查询失败！");
        }
        return R.ok().put("data", map);
    }

    //框选zhai------1
    @RequestMapping(value = "/getZhaiQueryFrame")
    @ResponseBody
    public R getZhaiQueryFrame(Integer pageSize, Integer currPage, String points) {
        try {
            Map map;
            SysUserEntity user = ShiroUtils.getUserEntity();
            map = pointService.getZhaiQueryFrame(pageSize, currPage, points, user.getCode());
            return R.ok().put("data", map);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return R.error("查询失败！");
        }
    }

    /**
     * 框选 zhai 导出使用
     *
     * @param points
     * @return
     */
    @RequestMapping(value = "/getExcelZhaiFrame")
    @ResponseBody
    public R getExcelZhaiFrame(String points) {
        try {
            Map map;
            SysUserEntity user = ShiroUtils.getUserEntity();
            map = pointService.getZhaiQueryFrame(null, null, points, user.getCode());
            return R.ok().put("data", map);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return R.error("查询失败！");
        }
    }

    /**
     * 查询户籍 性别 就业 户别 年龄范围 户籍成员 身份证号 明细------1
     *
     * @param xzqmc
     * @param pageSize
     * @param currPage
     * @param sexList
     * @param endAge
     * @param empList
     * @param startAge
     * @param huTList
     * @param housecount
     * @param idCard
     * @return
     */
    @RequestMapping(value = "/getHuQuery", method = RequestMethod.POST)
    @ResponseBody
    public R getHuQuery(String xzqmc, Integer pageSize, Integer currPage, Integer[] sexList, Integer[] empList,
                        Integer[] huTList, Integer startAge, Integer endAge, String housecount, String idCard) {
        Map map;
        try {
            ZhaiEntity zhai = new ZhaiEntity();
            if (sexList != null && sexList.length > 0) {
                List list = Arrays.asList(sexList);
                zhai.setSexList(list);
            }
            if (empList != null && empList.length > 0) {
                List list = Arrays.asList(empList);
                zhai.setEmpList(list);
            }
            if (huTList != null && huTList.length > 0) {
                List list = Arrays.asList(huTList);
                zhai.setHuTList(list);
            }
            if (startAge != null) {
                zhai.setStartAge(startAge);
            }
            if (endAge != null) {
                zhai.setEndAge(endAge);
            }
            if (housecount != null && !housecount.equals("")) {
                zhai.setSocialrelat(1);
                zhai.setHousecount(housecount);
            }
            if (idCard != null && !idCard.equals("")) {
                zhai.setIdCard(idCard);
            }
            SysUserEntity user = ShiroUtils.getUserEntity();
            List codeList = new ArrayList();
            //判断用户角色所在行政区 是否被包含
            if (xzqmc != null && !xzqmc.equals("")) {
                List<SysXzqEntity> xzqList = sysXzqService.queryXzqObject(xzqmc);
                for (SysXzqEntity x : xzqList) {
                    String str = x.getCode();
                    boolean status = str.contains(user.getCode());
                    if (status) {
                        codeList.add(str);
                    }
                }
                map = zhaiService.getHuQuery(pageSize, currPage, zhai, codeList);
            } else {
                codeList.add(user.getCode());
                map = zhaiService.getHuQuery(pageSize, currPage, zhai, codeList);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return R.error("查询失败！");
        }
        return R.ok().put("data", map);
    }

    /**
     * 户籍条件查询 统计------1
     *
     * @param xzqmc
     * @param sexList
     * @param empList
     * @param huTList
     * @param idCard
     * @param endAge
     * @param startAge
     * @param housecount
     * @return
     */
    @RequestMapping(value = "/getAppHuQuery", method = RequestMethod.POST)
    @ResponseBody
    public R getAppHuQuery(String xzqmc, Integer[] sexList, Integer[] empList, Integer[] huTList, Integer startAge, Integer endAge, String housecount, String idCard) {
        Map map;
        try {
            List codeList = new ArrayList();
            SysUserEntity user = ShiroUtils.getUserEntity();
            if (xzqmc != null && !xzqmc.equals("")) {
                List<SysXzqEntity> xzqList = sysXzqService.queryXzqObject(xzqmc);
                for (SysXzqEntity x : xzqList) {
                    String str = x.getCode();
                    boolean status = str.contains(user.getCode());
                    if (status) {
                        codeList.add(str);
                    }
                }
                map = pointService.getAppHuQuery(sexList, empList, huTList, startAge, endAge, housecount, idCard, codeList);
            } else {
                codeList.add(user.getCode());
                map = pointService.getAppHuQuery(sexList, empList, huTList, startAge, endAge, housecount, idCard, codeList);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return R.error("查询失败！");
        }
        return R.ok().put("data", map);
    }

    /**
     * 查询地籍 门牌号 权属性质 使用权类型 产权来源------1
     *
     * @param xzqmc
     * @param pageSize
     * @param currPage
     * @param cqlyList
     * @param qsxzList
     * @param syqlxList
     * @param mph
     * @return
     */
    @RequestMapping(value = "/getDiQuery")
    @ResponseBody
    public R getDiQuery(String xzqmc, Integer pageSize, Integer currPage, String mph, Integer[] qsxzList,
                        Integer[] syqlxList, Integer[] cqlyList) {
        Map map = new HashMap();
        try {
            YlEntity ylEntity = new YlEntity();
            if (mph != null && !mph.equals("")) {
                ylEntity.setMph(mph);
            }
            if (qsxzList != null && qsxzList.length > 0) {
                List list = Arrays.asList(qsxzList);
                ylEntity.setQsxzList(list);
            }
            if (syqlxList != null && syqlxList.length > 0) {
                List list = Arrays.asList(syqlxList);
                ylEntity.setSyqlxList(list);
            }
            if (cqlyList != null && cqlyList.length > 0) {
                List list = Arrays.asList(cqlyList);
                ylEntity.setCqlyList(list);
            }
            SysUserEntity user = ShiroUtils.getUserEntity();
            if (xzqmc != null && !xzqmc.equals("")) {
                List<SysXzqEntity> xzqList = sysXzqService.queryXzqObject(xzqmc);
                if (xzqList != null && xzqList.size() > 0) {
                    String str = xzqList.get(0).getCode();
                    boolean status = str.contains(user.getCode());
                    if (!status) {
                        return R.error("角色权限受限！");
                    }
                    map = ylService.getDiQuery(pageSize, currPage, ylEntity, xzqList.get(0).getCode());
                }
            } else {
                map = ylService.getDiQuery(pageSize, currPage, ylEntity, user.getCode());
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return R.error("查询失败！");
        }
        return R.ok().put("data", map);
    }

    /**
     * 地籍条件查询 统计-----1
     *
     * @param xzqmc
     * @param mph
     * @param qsxz
     * @param syqlx
     * @param cqly
     * @return
     */
    @RequestMapping(value = "/getAppDiQuery")
    @ResponseBody
    public R getAppDiQuery(String xzqmc, String mph, Integer[] qsxz, Integer[] syqlx, Integer[] cqly) {
        Map map = new HashMap();
        try {
            SysUserEntity user = ShiroUtils.getUserEntity();
            if (xzqmc != null && !xzqmc.equals("")) {
                List<SysXzqEntity> xzqList = sysXzqService.queryXzqObject(xzqmc);
                if (xzqList != null && xzqList.size() > 0) {
                    String str = xzqList.get(0).getCode();
                    boolean status = str.contains(user.getCode());
                    if (!status) {
                        return R.error("角色权限受限！");
                    }
                    map = pointService.getAppDiQuery(mph, qsxz, syqlx, cqly, xzqList.get(0).getCode());
                }
            } else {
                map = pointService.getAppDiQuery(mph, qsxz, syqlx, cqly, user.getCode());
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return R.error("查询失败！");
        }
        return R.ok().put("data", map);
    }

    /*
   * 点查 出负责人相关信息
   * */
    @RequestMapping("/queryBypointAll")
    public R queryBypointAll(String point){
        Map map = new HashMap();
        CjEntity cjEntity = cjService.queryBypointAll(point);
        DlhlEntity dlhlEntity = cjService.queryDlhl(point);
        FptbEntity fptbEntity = null;
        PyzlEntity pyzlEntity = null;
        SsnyEntity ssnyEntity = null;
        if (dlhlEntity == null){
            fptbEntity = cjService.queryFptb(point);
            pyzlEntity = cjService.queryPyzl(point);
            ssnyEntity = cjService.querySsny(point);
        }
        map.put("cjEntity",cjEntity);
        map.put("fptbEntity",fptbEntity);
        //map.put("jsydEntity",jsydEntity);
        map.put("pyzlEntity",pyzlEntity);
        map.put("ssnyEntity",ssnyEntity);
        map.put("dlhlEntity",dlhlEntity);
        return R.ok().put("data",map);
    }

    @RequestMapping("/queryMlxcBypoint")
    public R queryMlxcBypoint(String point){
        DlEntity dlEntity = cjService .queryDlBypoint(point);
        GfEntity gfEntity = cjService .queryGfBypoint(point);
        LhEntity lhEntity = cjService .queryLhBypoint(point);
        LhbwEntity lhbwEntity = cjService .queryLhbwBypoint(point);
        Map map = new HashMap();
        map.put("dlEntity",dlEntity);
        map.put("gfEntity",gfEntity);
        map.put("lhEntity",lhEntity);
        map.put("lhbwEntity",lhbwEntity);
        return R.ok().put("data",map);
    }

    @RequestMapping("/queryGeom")
    public R queryGeom(String code,String jsnr,String xmmc,String year,String lx){
        List<DlEntity> lhbwEntities = cjService.queryLhbw(code,jsnr,xmmc,year,lx);
        List<DlEntity> lhEntities = cjService.queryLh(code,jsnr,xmmc,year,lx);
        List<DlEntity> gfEntities = cjService.queryGf(code,jsnr,xmmc,year,lx);
        List<DlEntity> dlEntities = cjService.queryDl(code,jsnr,xmmc,year,lx);
        lhbwEntities.addAll(lhEntities);
        lhbwEntities.addAll(gfEntities);
        lhbwEntities.addAll(dlEntities);
        return R.ok().put("data",lhbwEntities);
    }

    @RequestMapping("/queryHjzzGeom")
    public R queryHjzzGeom(String code,String type,String ghry){
        List<HjzzEntity> fptbEntity = cjService.queryFptbGeom(code,type,ghry);
        List<HjzzEntity> pyzlEntity = cjService.queryPyzlGeom(code,type);
        List<HjzzEntity> ssnyEntity = cjService.querySsnyGeom(code,type);
        List<HjzzEntity> dlhlEntity = cjService.queryDlhlGeom(code,type,ghry);
        fptbEntity.addAll(pyzlEntity);
        fptbEntity.addAll(ssnyEntity);
        fptbEntity.addAll(dlhlEntity);
        return R.ok().put("data",fptbEntity);
    }

    /*
   * 宅基地 环保 框查
   * */
    @RequestMapping("/queryZjdCount")
    public R queryZjdCount(@RequestBody Map map){
        String point = null;
        if (map != null && map.get("points") != null){
            point = map.get("points").toString();
        }
        String code = null;
        if (map != null && map.get("code") != null){
            code = map.get("code").toString();
        }
        Map map1;
        try {
            map1 = pointService.queryZjdCount(point,code);
        }catch (Exception e){
            return R.error("查询失败！");
        }
        return R.ok().put("data",map1);
    }

    /*
    * 查询地块 道路信息
    * */
    @RequestMapping("/queryDkDl")
    public R queryDkDl(String point){
        Map map = new HashMap();
        String geome = null;
        RjdlEntity rjdlEntity = cjService.queryYDl(point);
        map.put("rjdlEntity",rjdlEntity);
        RjhjhpEntity rjhjhpEntity = null;
        RjriverEntity  rjriverEntity = null;
        if (rjdlEntity == null){
            rjhjhpEntity = cjService.queryGxys(point);
            if(rjhjhpEntity != null){
                geome = rjhjhpEntity.getGeom();
                if(rjhjhpEntity.getType().equals("农用地2")){
                    rjhjhpEntity = cjService.queryNyd(point);
                }
            }
            rjriverEntity = cjService.queryRiver(point);
            if (rjriverEntity != null){
                geome = rjriverEntity.getGeom();
            }
        }else {
            geome = rjdlEntity.getGeom();
        }
        map.put("rjhjhpEntity",rjhjhpEntity);
        map.put("rjriverEntity",rjriverEntity);
        List<BplxEntity> bplxEntitys = new ArrayList<>();
        if (rjhjhpEntity != null && rjhjhpEntity.getCode() != null){
            bplxEntitys = cjService.queryBplx(rjhjhpEntity.getCode());
        }
        if (rjriverEntity != null && rjriverEntity.getCode() != null){
            bplxEntitys = cjService.queryBplx(rjriverEntity.getCode());
        }
        if (rjdlEntity != null  && rjdlEntity.getCode() != null){
            bplxEntitys = cjService.queryBplx(rjdlEntity.getCode());
        }
        map.put("bplxEntity",bplxEntitys);
        //查询环境整治信息
        List<PjEnviorSupvsEntity> enviorSupvsEntities = enviorSupvsService.queryBygeom(geome);
        map.put("enviorSupvsEntities",enviorSupvsEntities);
        return R.ok().put("data",map);
    }

    //添加 保洁人员 街巷长
    @RequestMapping("/updateMlxz")
    @Transactional
    public void updateMlxz(){
        List<String> codes = cjService.queryBjryAll();
        for (String code : codes) {
            List<RjhjhpEntity> gxys8Entities = cjService.queryAll(code);
            if (gxys8Entities != null && gxys8Entities.size() > 0) {
                for (RjhjhpEntity gxys8Entity : gxys8Entities) {
                    //判断保洁人员是否为空  空则添加
                    if (gxys8Entity.getBjry() == null || "".equals(gxys8Entity.getBjry())) {
                        //查找保洁人员
                        BplxEntity bplxEntity = cjService.queryBjry(code);
                        if (bplxEntity != null) {
                            gxys8Entity.setBjry(bplxEntity.getName() == null ? "" : bplxEntity.getName());
                            gxys8Entity.setBjdz(bplxEntity.getCaptain() == null ? "" : bplxEntity.getCaptain());
                            gxys8Entity.setBjlx(bplxEntity.getDzlx() == null ? "" : bplxEntity.getDzlx());
                            //修改保洁人员
                            cjService.updateGxys(gxys8Entity);
                            //修改次数
                            cjService.updateBjry(bplxEntity);
                        }
                    }
                }
            }
            List<RjdlEntity> rjdlEntities = cjService.queryDlAll(code);
            if (rjdlEntities != null && rjdlEntities.size() > 0) {
                for (RjdlEntity rjdlEntity : rjdlEntities) {
                    //判断保洁人员是否为空  空则添加
                    if (rjdlEntity.getBjry() == null || "".equals(rjdlEntity.getBjry())) {
                        //查找保洁人员
                        BplxEntity bplxEntity = cjService.queryBjry(code);
                        if (bplxEntity != null) {
                            rjdlEntity.setBjry(bplxEntity.getName() == null ? "" : bplxEntity.getName());
                            rjdlEntity.setBjdz(bplxEntity.getCaptain() == null ? "" : bplxEntity.getCaptain());
                            rjdlEntity.setBjlx(bplxEntity.getDzlx() == null ? "" : bplxEntity.getDzlx());
                            //修改次数
                            cjService.updateBjry(bplxEntity);
                        }
                    }
                    if (rjdlEntity.getJxz() == null || "".equals(rjdlEntity.getJxz())) {
                        //查找街巷长
                        BplxEntity bplxEntity1 = cjService.queryJxz(code);
                        if (bplxEntity1 != null) {
                            rjdlEntity.setJxz(bplxEntity1.getName());
                            rjdlEntity.setJxzlx(bplxEntity1.getPhone());
                            cjService.updateJxz(bplxEntity1);
                        }
                    }
                    cjService.updateRjdl(rjdlEntity);
                }
            }
        }
    }

}
